package com.ten.order.service.impl;

import com.ten.order.dao.TOrderDao;
import com.ten.order.service.TOrderService;
import com.ten.order.vo.OrderVo;
import com.ten.order.vo.PayLogVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ten.common.core.entity.TOrder;
import com.ten.common.core.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


/**
 * @author ：王薪伟
 * @date ：Created in 2022/12/7 18:30
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class TOrderServiceImpl implements TOrderService {

    @Autowired
    private TOrderDao tOrderDao;

    //如果使用map分页 需要配置分页拦截器
    @Override
    public Result<IPage<TOrder>> findAllOrderByPage(Integer currentPage, Integer pageSize, OrderVo orderVo) {
        IPage<TOrder> page = new Page(currentPage, pageSize);

        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();

        if (StringUtils.hasText(orderVo.getOrderNo())) {
            wrapper.like("order_no", orderVo.getOrderNo());
        }

        if (StringUtils.hasText(orderVo.getMobile())) {
            wrapper.like("mobile", orderVo.getMobile());
        }

        if (StringUtils.hasText(orderVo.getCourseTitle())) {
            wrapper.like("course_title", orderVo.getCourseTitle());
        }

        if (orderVo.getStatus() != null) {
            wrapper.like("status", orderVo.getStatus());
        }

        if (StringUtils.hasText(orderVo.getTeacherName())) {
            wrapper.like("teacher_name", orderVo.getTeacherName());
        }

        if (orderVo.getPayType() != null) {
            wrapper.like("pay_type", orderVo.getPayType());
        }

        if (orderVo.getGmtCreate() != null) {
            wrapper.ge("gmt_create", orderVo.getGmtCreate());
        }

        if (orderVo.getGmtModified() != null) {
            wrapper.le("gmt_modified", orderVo.getGmtModified());
        }

        IPage<TOrder> iPage = tOrderDao.selectPage(page, wrapper);

        return new Result<>(2000, "查询成功", iPage);
    }

    @Override
    public Result<PayLogVo> findAllOrder(String orderNo) {
        PayLogVo payLogVo = tOrderDao.selectOrderAndPayLog(orderNo);
        return new Result<PayLogVo>(2000, "查询成功", payLogVo);
    }

    @Override
    public Result deleteOrderById(String id) {
        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        int delete = tOrderDao.delete(wrapper);
        if (delete == 1) {
            return new Result(2000, "取消订单成功");
        }
        return new Result(5000, "取消订单失败");
    }

    @Override
    public Result findOrderStatus(String courseId, String memberId) {
        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        wrapper.eq("member_id", memberId);
        TOrder tOrder = tOrderDao.selectOne(wrapper);
        if (tOrder != null) {
            if (tOrder.getStatus() == 0) {
                return new Result(2000, "未购买该课程", null);
            }
            return new Result(2000, "课程已购买", tOrder);
        }
        return new Result(2000, "未购买该课程", null);
    }

}
